<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/22
 * Time: 14:16
 */

namespace Modules\Web\Service;
use HiCommon\Repository\ShopMasterRepository;

class ShopMasterService
{

    /**
     *  获取门店的所有技师列表
     *  hinq 2019 11 22
     *
     **/
    public function shop_master(){
        try{
            $repairstore_id = get_web_shop_id();   //当前商户的商户id
            $arr_option['where'] = ['shop_id'=>$repairstore_id];
            $arr_list = ShopMasterRepository::get_list( (array) $arr_option )->toArray();
            $data = list_page_data_for_api( $arr_list );
            return hello_success('ok',$data);
        }catch ( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }


    /**
     *  获取指定技师信息
     *  hinq 2019 11 22
     *
     **/
    public function shop_master_detail( int $nId ){
        try{
            $arr_list = ShopMasterRepository::get_one( (int) $nId );
            return hello_success('ok',$arr_list);
        }catch ( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  数据验证
     *  hinq 2019 11 22
     *
     **/
    public function check_data( array $arr_input ){
        if( empty( $arr_input['img'] ) ){
            return hello_error('头像不能为空');
        }
        if( empty( $arr_input['name'] ) ){
            return hello_error('技师姓名不能为空');
        }
        if( empty( $arr_input['phone'] ) ){
            return hello_error('技师手机号码不能为空');
        }
        return $arr_input;
    }

    /**
     *  添加门店的技师信息
     *  hinq 2019 11 22
     *
     **/
    public function shop_master_store( array $arr_input ){
        try{
            $temp = self::check_data( (array) $arr_input );

            if( $temp['state'] == 1 ){
                return $temp;
            }
            $repairstore_id = get_web_shop_id();   //当前商户的商户id
            $data['shop_id'] = $repairstore_id;
            $data['img'] = $arr_input['img'];
            $data['name'] = $arr_input['name'];
            $data['phone'] = $arr_input['phone'];
            $res = ShopMasterRepository::add_one((array)$data);
            if (empty($res)) {
                return hello_error('添加失败');
            }
            return hello_success('ok');
        }catch ( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  更新门店指定的技师信息
     *  hinq 2019 11 22
     *
     **/
    public function shop_master_update( array $arr_input ){
        try{
            $temp = self::check_data( (array) $arr_input );
            if( $temp['state'] == 1 ){
                return $temp;
            }
            $data['img'] = $arr_input['img'];
            $data['name'] = $arr_input['name'];
            $data['phone'] = $arr_input['phone'];
            $data['id'] = $arr_input['id'];
            $res = ShopMasterRepository::edit_one( (array) $data );
            if( empty( $res ) ){
                return hello_error('编辑失败');
            }
            return hello_success('ok');
        }catch ( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }

    /**
     *  删除指定的技师信息
     *  hinq 2019 11 22
     *
     **/
    public function shop_master_del( int $nId )
    {
        try{
            if (empty($nId)) {
                return hello_error('参数错误');
            }
            $res = ShopMasterRepository::delete_one((int)$nId);
            if (empty($res)) {
                return hello_error('删除失败');
            }
            return hello_success('ok');
        }catch ( \Exception $exception ){
            return ['state'=>1,'msg'=>$exception->getMessage()];
        }
    }




}
